package it.antonio.ilpizze.swing.session;

import java.awt.Component;
import java.awt.Rectangle;
import java.io.Serializable;

import javax.swing.JFrame;

public class SessionStateCreator implements Serializable{
	
	private static final long serialVersionUID = 1L;

	public <T extends Component> SessionState<T> create(Class<? extends Component> clazz){
		if(JFrame.class.isAssignableFrom(clazz)){
			return new JFrameSessionState<T>();
		}
		
		return new DefaultSessionState<T>();
	} 
	
	public class DefaultSessionState<T extends Component> implements SessionState<T> {

		private static final long serialVersionUID = 1L;
		private Rectangle bounds;
		
		@Override
		public void save(T component) {
			bounds = component.getBounds();
		}

		@Override
		public void restore(T component) {
			if(bounds != null){
				component.setLocation(bounds.getLocation());
				component.setSize(bounds.getSize());
			}
		}

	}

	public class JFrameSessionState<T extends Component> implements SessionState<T> {

		private static final long serialVersionUID = 1L;
		private Rectangle bounds;
		
		@Override
		public void save(T component) {
			bounds = component.getBounds();
		}

		@Override
		public void restore(T component) {
			if(bounds != null){
				component.setBounds(bounds);
			}
		}

	}
}
